package ufo

import api.action.{Action, EndTurn, Walk}
import api.common.{East, Position, West, North}
import api.event.Event
import api.{ActionPerformer, EventListener}
import client.{LazyloadActionPerformer, UfoClient}
import java.util.UUID
import server.{ServerBeing, Client, World}

object Main extends Application {
  implicit val world = new World(10, 10)

  def newClient(name : String) = {
    val lazyAP = new LazyloadActionPerformer
    val ufoClient = new UfoClient(name, new SerializedActionPerformer(lazyAP))
    val client = new Client(UUID.randomUUID(), new SerializedEventListener(ufoClient))

    lazyAP.set(client)
    world.addClient(client)

    client
  }


  val client = newClient("1")
  val client2 = newClient("2")

  world.add(new ServerBeing(UUID.randomUUID(), client, Position(4, 0), 10))
  world.add(new ServerBeing(UUID.randomUUID(), client, Position(2, 2), 10))
  world.add(new ServerBeing(UUID.randomUUID(), client2, Position(9, 7), 10))

  world.newTurn()

/*
  val client2 = new Client(UUID.randomUUID(), new PrintEventListener("2"))
  world.addClient(client2)
  world.addClient(client)

  val beingId = UUID.randomUUID()

  world.add(new ServerBeing(beingId, client, Position(4, 0)))
  world.newTurn()
  
  client.perform(Walk(beingId, West))
  client.perform(Walk(beingId, West))
  client.perform(Walk(beingId, West))
  client2.perform(Walk(beingId, West))
  client.perform(EndTurn())
  client2.perform(Walk(beingId, West))
  client2.perform(EndTurn())
  client.perform(Walk(beingId, East))
  */

}

class SerializedEventListener(delegate : EventListener) extends EventListener with SerializedIndirection {
  def notify(event : Event) = {
    delegate.notify(serialize(event))
  }
}

class SerializedActionPerformer(delegate : ActionPerformer) extends ActionPerformer with SerializedIndirection {
  def perform(action : Action) = {
    delegate.perform(serialize(action))
  }
}